﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Utilities.Encoders;
using System.Net;
using up6.db.model;
using up6.utils;
using Up6.core.Up6Manager;
using Up6.core.Up6Manager.Models.Req;


namespace Up6.core.Controllers;

[ApiController]
[Route("")]
public class MgrController : ControllerBase
{
    private readonly MgrAction act;
    private readonly IConfiguration _configuration;
    public MgrController(MgrAction up6Service, IConfiguration configuration)
    {
        act = up6Service;
        _configuration = configuration;
    }

    [HttpGet]
    [Route("api/filemgr/data")]
    public dynamic data(        
        [FromQuery] string uid,        
        [FromQuery] string pathRel=""       
        )
    {
        var f = new FileInf();        
        f.uid = uid;
        f.pathRel = pathRel;
        f.pathRel = WebUtility.UrlDecode(pathRel);        
        return act.Data(f);

    }

    [HttpGet]
    [Route("api/filemgr/del")]

    public dynamic del(
        [FromQuery] string id,
        [FromQuery] string pathRel=""        
        )
    {
        var f = new FileInf();
        f.id = id;        
        f.pathRel = pathRel;
        f.pathRel = WebUtility.UrlDecode(pathRel);
        return act.del(f);

    }

    [HttpGet]
    [Route("api/filemgr/f_create")]
    public async Task<IActionResult> createAsync(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string md5,
        [FromQuery] long lenLoc,
        [FromQuery] long lenLocSec,
        [FromQuery] string sizeLoc,
        [FromQuery] string callback,
        [FromQuery] string pathLoc,
        [FromQuery] string pathRel,
        [FromQuery] int blockSizeSec=0,
        [FromQuery] int blockSize = 5242880,
        [FromQuery] string encryptAgo="",        
        [FromQuery] string pid = "",
        [FromQuery] string pidRoot = ""
        )
    {
        if (string.IsNullOrEmpty(pidRoot)) pidRoot = pid;
        var f = new FileInf();
        f.pid= pid;
        f.pidRoot= pidRoot;
        f.fdChild = false;
        f.md5 = md5;
        f.id= id;
        f.uid= uid;
        f.lenLoc= lenLoc;
        f.lenLocSec= lenLocSec;
        f.sizeLoc = sizeLoc;
        f.blockSize = blockSize;//块大小
        f.blockSizeSec = blockSizeSec;//加密块大小
        f.encryptAgo = encryptAgo;//加密算法
        f.encrypt = ConfigReader.storageEncrypt();//存储加密        
        f.callback = callback;
        f.pathLoc = pathLoc;
        f.pathLoc = WebUtility.UrlDecode(pathLoc);
        f.nameLoc = Path.GetFileName(f.pathLoc);
        f.nameSvr = f.nameLoc;        
        f.pathRel = WebUtility.UrlDecode(pathRel);
        bool isRootFile = f.pathRel == "/";
        f.fdChild = !isRootFile;
        f.pathRel = PathTool.combin(f.pathRel, f.nameLoc);
        f.deleted = false;

        return await act.CreateAsync(f);

    }

    [HttpGet]
    [Route("api/filemgr/f_complete")]

    public dynamic complete(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] int cover,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid= uid;
        f.cover = cover;
        f.callback = callback;
        return act.complete(f);

    }

    [HttpGet]
    [Route("api/filemgr/fd_create")]

    public dynamic fd_create(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] long lenLoc,
        [FromQuery] string sizeLoc,
        [FromQuery] string pathLoc,
        [FromQuery] string pathRel,
        [FromQuery] string callback,
        [FromQuery] string pid = ""
        )
    {
        var f = new FileInf();
        f.id= id;
        f.pid= pid;
        f.pidRoot= "";
        f.fdChild = false;
        f.fdTask = true;
        f.uid = uid;
        f.pathLoc=WebUtility.UrlDecode(pathLoc);
        f.nameLoc = Path.GetFileName(f.pathLoc);
        f.nameSvr = f.nameLoc;
        f.pathRel = WebUtility.UrlDecode(pathRel);        
        f.lenLoc= lenLoc;
        f.sizeLoc = sizeLoc;
        f.deleted = false;
        f.callback= callback;
        return act.fd_create(f);
    }

    [HttpGet]
    [Route("api/filemgr/fd_complete")]

    public dynamic fd_complete(
        [FromQuery] string id,
        [FromQuery] string uid,        
        [FromQuery] int cover,
        [FromQuery] string callback,
        [FromQuery] string pid = "",
        [FromQuery] string pathRel = ""
        )
    {
        var f = new FileInf();
        f.id = id;
        f.pid = pid;
        f.uid = uid;
        f.pathRel = WebUtility.UrlDecode(pathRel);
        f.cover = cover;
        f.callback = callback;
        return act.fd_complete(f);
    }

    [HttpGet]
    [Route("api/filemgr/fd_data")]

    public dynamic fd_data(
        [FromQuery] string id,
        [FromQuery] string callback,
        [FromQuery] string pid=""        
        )
    {
        var f = new FileInf();
        f.id = id;
        f.pid = pid;        
        f.callback = callback;
        return act.fd_data(f);
    }

    [HttpGet]
    [Route("api/filemgr/mkdir")]

    public dynamic mkdir(
        [FromQuery] string uid,
        [FromQuery] string nameLoc,
        [FromQuery] string pid="",        
        [FromQuery] string pidRoot="",        
        [FromQuery] string pathRel=""        
        )
    {
        var f = new FileInf();        
        f.uid = uid;
        f.pid = pid;
        f.pidRoot = pidRoot;
        f.nameLoc = WebUtility.UrlDecode(nameLoc);
        f.pathRel = WebUtility.UrlDecode(pathRel);
        return act.mkdir(f);
    }

    [HttpGet]
    [Route("api/filemgr/path")]

    public dynamic path(
        [FromQuery] string data        
        )
    {        
        return act.path(data);
    }

    [HttpGet]
    [Route("api/filemgr/rename")]

    public dynamic rename(
        [FromQuery] string id,
        [FromQuery] bool fdTask,        
        [FromQuery] string nameLoc,
        [FromQuery] string pathRel=""
        )
    {
        var f = new FileInf();
        f.id = id;
        f.fdTask = fdTask;        
        f.nameLoc = WebUtility.UrlDecode(nameLoc);
        f.pathRel = pathRel;
        return act.rename(f);
    }

    [HttpGet]
    [Route("api/filemgr/search")]

    public dynamic search(
        [FromQuery] string uid,                
        [FromQuery] string pid = "",
        [FromQuery] string key = "",
        [FromQuery] string pathRel = ""
        )
    {
        var f = new FileInf();
        f.uid = uid;
        f.pid = pid;
        f.pathRel = WebUtility.UrlDecode(pathRel);
        f.key = WebUtility.UrlDecode(key);
        return act.search(f);
    }

    [HttpGet]
    [Route("api/filemgr/uncmp-down")]

    public dynamic uncmp_down(
        [FromQuery] string uid        
        )
    {
        var f = new FileInf();
        f.uid = uid;        
        return act.uncmp_down(f);
    }

    [HttpGet]
    [Route("api/filemgr/uncomp")]

    public dynamic uncomp(
        [FromQuery] string uid
        )
    {
        var f = new FileInf();
        f.uid = uid;
        return act.uncomp(f);
    }
}
